package com.ddm.service.impl.vxImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddm.entity.vxEntity.Collection;
import com.ddm.mapper.CollectionMapper;
import com.ddm.service.vxService.CollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Override
    public boolean isQuestionCollected(Long userId, Long questionId) {
        if (userId == null || questionId == null) {
            return false;
        }
        
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collection::getUserId, userId)
               .eq(Collection::getQuestionId, questionId);
        
        return this.count(wrapper) > 0;
    }

    @Override
    public List<Long> getCollectedQuestionIds(Long userId) {
        if (userId == null) {
            return List.of();
        }
        
        // 使用更简单的方式，先获取所有记录，然后提取questionId
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collection::getUserId, userId);
        
        List<Collection> collections = this.list(wrapper);
        
        return collections.stream()
                .map(Collection::getQuestionId)
                .collect(Collectors.toList());
    }

    @Override
    public boolean cancelCollection(Long userId, Long questionId) {
        log.info("开始取消收藏，用户ID: {}, 题目ID: {}", userId, questionId);
        
        if (userId == null || questionId == null) {
            log.warn("参数为空，用户ID: {}, 题目ID: {}", userId, questionId);
            return false;
        }
        
        // 先检查收藏记录是否存在
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collection::getUserId, userId)
               .eq(Collection::getQuestionId, questionId);
        
        Collection collection = this.getOne(wrapper);
        if (collection == null) {
            log.warn("收藏记录不存在，用户ID: {}, 题目ID: {}", userId, questionId);
            return false; // 收藏记录不存在
        }
        
        log.info("找到收藏记录，记录ID: {}", collection.getId());
        
        // 收藏记录存在，执行删除操作
        boolean removed = this.removeById(collection.getId());
        log.info("删除操作结果: {}, 记录ID: {}", removed, collection.getId());
        
        // 如果removeById返回false，但记录确实存在，我们再次检查记录是否真的被删除了
        if (!removed) {
            Collection checkCollection = this.getById(collection.getId());
            if (checkCollection == null) {
                log.info("虽然removeById返回false，但记录已被删除，认为删除成功");
                return true;
            } else {
                log.warn("removeById返回false，且记录仍然存在，删除失败");
                return false;
            }
        }
        
        return true;
    }
}
